<?php
/**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */

/**
 * This file generate Bar chart graph and displays it to browser
 */

/**
 * Setting the chart variable
 */
session_start();
$graphHeader = $_SESSION['graphInput']['title'];
$xAxisLabel		= $_SESSION['graphInput']['x-label'];
$yAxisLabel 	= $_SESSION['graphInput']['y-label'];

$data = $_SESSION['graphInput']['data']; 
/**
 * getting the maximum and minimum values for Y
 */
$newData = $data;
asort($newData);

//minimum
$cordinates = strlen(current($newData));
$mod    = pow(10, $cordinates-1);
$min    = $mod - current($newData);
if($min < 0){$min = 0;}
 
//maximum
$cordinates = strlen(end($newData));
$mod    = pow(10, $cordinates-1);
$max 	= $mod + end($newData); 
 
//storing those min and max values into an array
$yAxis  = array("min"=>$min, "max"=>$max);

//setting the image dimensions
$canvasWidth  = 500;
$canvasHeight = 300;
$perimeter    = 50; 
 
//creating the canvas
$canvas = imagecreatetruecolor($canvasWidth, $canvasHeight); 
 
//allocating the colors
$white     = imagecolorallocate($canvas, 255, 255, 255);
$black     = imagecolorallocate($canvas, 0,0,0);
$yellow    = imagecolorallocate($canvas, 248, 255, 190);
$blue      = imagecolorallocate($canvas, 3,12,94);
$grey      = imagecolorallocate($canvas, 102, 102, 102);
$lightGrey = imagecolorallocate($canvas, 216, 216, 216); 
 
//getting the size of the fonts
$fontwidth  = imagefontwidth(2);
$fontheight = imagefontheight(2); 
 
//filling the canvas with light grey
imagefill($canvas, 0,0, $lightGrey);


//getting the size of the grid
$gridWidth  = $canvasWidth  - ($perimeter*2);
$gridHeight = $canvasHeight - ($perimeter*2); 
 
//getting the grid plane coordinates
$c1 = array("x"=>$perimeter, "y"=>$perimeter);
$c2 = array("x"=>$gridWidth+$perimeter, "y"=>$perimeter);
$c3 = array("x"=>$gridWidth+$perimeter, "y"=>$gridHeight+$perimeter);
$c4 = array("x"=>$perimeter, "y"=>$gridHeight+$perimeter);

imagefilledrectangle($canvas, $c1['x'], $c1['y'], $c3['x'], $c3['y'], $white);  
 
//finding the size of the grid squares
$sqW = $gridWidth/count($data);
$sqH = $gridHeight/$yAxis['max']; 

$verticalPadding = $sqW/2;
$increment = 0;
foreach($data as $assoc=>$value)
{
	//drawing the line
	imageline($canvas, $verticalPadding+$c4['x']+$increment, $c4['y'], $verticalPadding+$c1['x']+$increment, $c1['y'], $black);
 
	//axis values
	$wordWidth = strlen($assoc)*$fontwidth;
	$xPos = $c4['x']+$increment+$verticalPadding-($wordWidth/2);
	ImageString($canvas, 2, $xPos, $c4['y'], $assoc, $black);
 
	$increment += $sqW;
}

//resetting the increment back to 0
$increment = 0; 
 
for($i=$yAxis['min']; $i<$yAxis['max']; $i++)
{
	if($i%$mod==0){
		//drawing the line
		imageline($canvas, $c4['x'], $c4['y']+$increment, $c3['x'], $c3['y']+$increment, $black);
 
		//axis values
		$xPos = $c1['x']-($fontwidth*strlen($i))-5;
		ImageString($canvas, 2, $xPos, $c4['y']+$increment-($fontheight/2), $i, $black);
 
	}
	elseif(($mod/5)>1 && $i%($mod/5)==0)
	{
		imageline($canvas, $c4['x'], $c4['y']+$increment, $c4['x']+10, $c4['y']+$increment, $grey);
	}
	$increment-=$sqH;
}

//getting the size of the grid
$gridWidth  = $canvasWidth  - ($perimeter*2);
$gridHeight = $canvasHeight - ($perimeter*2);

//getting the grid plane coordinates
$c1 = array("x"=>$perimeter, "y"=>$perimeter);
$c2 = array("x"=>$gridWidth+$perimeter, "y"=>$perimeter);
$c3 = array("x"=>$gridWidth+$perimeter, "y"=>$gridHeight+$perimeter);
$c4 = array("x"=>$perimeter, "y"=>$gridHeight+$perimeter);

imagefilledrectangle($canvas, $c1['x'], $c1['y'], $c3['x'], $c3['y'], $white);

//finding the size of the grid squares
$sqW = $gridWidth/count($data);
$sqH = $gridHeight/$yAxis['max'];

$increment = 0; 
$barWidth = $sqW*.2;
foreach($data as $assoc=>$value)
{
	$yPos = $c4['y']-($value*$sqH);
	$xPos = $c4['x']+$increment+$verticalPadding-($barWidth/2);
	imagefilledrectangle($canvas, $xPos, $c4['y'], $xPos+$barWidth, $yPos, $blue);
	$increment += $sqW;
}
	
$green = "#00FF00";
//Graph Title
ImageString($canvas, 5, ($canvasWidth/2)-(strlen($graphHeader)*$fontwidth)/2, $c1['y']-($perimeter/2), $graphHeader, $green); 
 
//X-Axis
ImageString($canvas, 5, ($canvasWidth/2)-(strlen($xAxisLabel)*$fontwidth)/2, $c4['y']+($perimeter/2), $xAxisLabel, $green); 
 
//Y-Axis
ImageStringUp($canvas, 5, $c1['x']-$fontheight*3, $canvasHeight/2+(strlen($yAxisLabel)*$fontwidth)/2, $yAxisLabel, $green);


header("content-type: image/jpg");
imagejpeg($canvas);
imagedestroy($canvas);
?>